package SA.teacher.manage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
import SA.teacher.*;
import SA.teacher.answer.AnswerActivity1;
import SA.teacher.daily.*;
import SA.teacher.notice.NoticeActivity1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.*;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class ManageActivity1 extends Activity {

	ListView list;
	MyAdapter adapter;
	ArrayList<ManageData> arrData;
	static String rcvid;
	String getClassName;
	static String id;
	String rcvclassid;
	String day = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage1);

		Intent intent = getIntent();

		id = intent.getStringExtra("ID");
		getClassName = intent.getStringExtra("classname");
		TextView className = (TextView) findViewById(R.id.className);
		className.setText(getClassName + " 반");

		setListData();

		adapter = new MyAdapter(this, arrData, id);

		list = (ListView) findViewById(R.id.list);
		list.setAdapter(adapter);


		//////////////Button action////////////////////////////////////
		TextView noticeBtn = (TextView) findViewById(R.id.notice);
		noticeBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				noticeActivity();
			}
		});

		ImageView mainBtn = (ImageView) findViewById(R.id.mainBtn);
		mainBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mainActivity();
			}
		});

		ImageView dailyBtn = (ImageView) findViewById(R.id.dailyBtn);
		dailyBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailyActivity();
			}
		});
		ImageView answerBtn = (ImageView) findViewById(R.id.answerBtn);
		answerBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				answerActivity();	
			}
		});
	}

	private void setListData() {
		arrData = new ArrayList<ManageData>();
		listparse idps = new listparse();
		idps.run();
		final String[] idlist = rcvid.split(",");
		int i;
		int temp = Integer.parseInt(idlist[0]);//학생수 스트링 형변환

		for(i=1; i < temp; i++)
		{
			String[] divide =idlist[i].split(">");
			arrData.add(new ManageData(R.drawable.ic_launcher, divide[0], divide[1],
					R.drawable.roundbtn, R.drawable.roundbtn));
		}
	}





	/////////////파싱//////////////

	private class listparse implements Runnable {
		public void run() {
			// TODO Auto-generated method stub
			try {
				URL url = new URL("Http://220.67.128.8/studentlist.php?classname=" +URLEncoder.encode(getClassName,"EUC-KR"));
				Log.e("1", getClassName);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();

				InputStream  is   = null;
				ByteArrayOutputStream baos = null; 
				if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
					is = conn.getInputStream();  
					baos = new ByteArrayOutputStream();
					byte[] byteBuffer = new byte[1024];    byte[] byteData = null; 
					int nLength = 0;
					while((nLength = is.read(byteBuffer)) > 0) {   
						baos.write(byteBuffer, 0, nLength); 
					}
					byteData = baos.toByteArray(); 
					String response = new String(byteData, "euc-k-r"); 
					rcvid=response;
					if(response==null || response.equals("")) {    
						throw new Exception("Response is NULL!");  
					}
				}
				else{
					Log.e("login 응답 : ", "접속 안됨");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}
/*
	private class classidparse implements Runnable {
		public void run() {	Log.e("login 응답 : ", id);
		// TODO Auto-generated method stub
		try {
			URL url = new URL("Http://220.67.128.8/dayclassid.php?id="+ URLEncoder.encode(id, "EUC-KR"));
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			InputStream is = null;
			ByteArrayOutputStream baos = null;
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				is = conn.getInputStream();
				baos = new ByteArrayOutputStream();
				byte[] byteBuffer = new byte[1024];
				byte[] byteData = null;
				int nLength = 0;
				while ((nLength = is.read(byteBuffer)) > 0) {
					baos.write(byteBuffer, 0, nLength);
				}
				byteData = baos.toByteArray();
				String response = new String(byteData, "euc-k-r");
				rcvclassid = response;
				if (response == null || response.equals("")) {
					throw new Exception("Response is NULL!");
				}
			} else {
				Log.e("login 응답 : ", "접속 안됨");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
		}
	}*/
	//////////////Button 함수들////////////////////////////////////
	void noticeActivity() {
		Intent intent = new Intent(this, NoticeActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void mainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void dailyActivity() {
		Intent intent = new Intent(this, DailyActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void answerActivity() {
		Intent intent = new Intent(this, AnswerActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}

	//////////////뒤로가기 종료////////////////////////////////////
	public boolean onKeyDown(int keyCode, KeyEvent event){
		switch(keyCode){
		case KeyEvent.KEYCODE_BACK:
			String alertTitle = getResources().getString(R.string.app_name);
			String buttonMessage = getResources().getString(R.string.alert_msg_exit);
			String buttonYes = getResources().getString(R.string.button_yes);
			String buttonNo = getResources().getString(R.string.button_no);

			new AlertDialog.Builder(ManageActivity1.this)
			.setTitle(alertTitle)
			.setMessage(buttonMessage)
			.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					moveTaskToBack(true);
					finish();
				}
			})
			.setNegativeButton(buttonNo, null)
			.show();
		}
		return true;
	}
}
